<?php
/**
 * Created by PhpStorm.
 * User: Jython
 * Date: 2018/3/8
 * Time: 0:09
 */
ini_set('date.timezone','Asia/Shanghai');
function p($str){
    echo "<pre>";
    print_r($str);
    exit;
}
$website_path='/upload-pic/upload/'.date('Y-m-d').'/';
$pic_path_dir=$_SERVER['DOCUMENT_ROOT'].$website_path;
if(!is_dir($pic_path_dir)&&!mkdir($pic_path_dir,0777,true)){
    exit('创建失败');
}
$file=$_FILES['file'];
$name=$file['name'];
$type=strtolower(substr($name,strrpos($name,'.')+1));
$allow_type=['jpg','jpeg','gif','png'];

if(!in_array($type,$allow_type)){
    exit('type error');
}

if(!is_uploaded_file($file['tmp_name'])){
    exit('not http');
}
$shuffle_str=substr(str_shuffle('abcdefjhijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ1234567890'),0,10);
$file_new_name=$shuffle_str.'.'.$type;
$file_path=$pic_path_dir.$file_new_name;
$file_src_path=$website_path.$file_new_name;
if(!move_uploaded_file($file['tmp_name'],$file_path)){
    exit('移动失败');
}

exit(json_encode(['code'=>0,'msg'=>'ok','data'=>['src'=>$file_src_path]]));
exit('ok');

